/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Supplier.java
 *
 * Created on Oct 27, 2010, 4:59:46 PM
 */

package lmclogistics;

import components.Text;
import components.TextArea;
import components.Image;
import components.NButton;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import sql.Sql;

/**
 *
 * @author Sonia Tan
 */
public class EditSupplier extends javax.swing.JFrame {

    /** Creates new form Supplier */
    private Image bg;
    private int id;

    Text cd = new Text();
    TextArea name = new TextArea();
        TextArea terms = new TextArea();
        TextArea begBal = new TextArea();
        TextArea apBal = new TextArea();
        TextArea address = new TextArea();
        TextArea contactPerson = new TextArea();
        TextArea telNo = new TextArea();
        TextArea fax = new TextArea();

    public EditSupplier(int sID) {
        id = sID;

        setLayout(null);
        setTitle("Edit Supplier");
        setAlwaysOnTop(true);
        setSize(512, 488);
        setResizable(false);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);     
        centerScreen();
        setVisible(true);

        setBg();
        okCancelButtons();
        initText();
        initFlds();

        BufferedImage image = null;
        try {
            image = ImageIO.read(getClass().getResource("/images/favicon.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        setIconImage(image);
    }

    public final void setBg(){
        bg = new Image("panel.png",509,460);

        add(bg,-1);

    }

    public int getCenterX(int x){

        int a = 919/2 - x/2;


        return a;
    }

    public int getCenterY(int x){

        int a = 577/2 - x/2;


        return a;
    }

    public void okCancelButtons()
    {
        NButton cancel = new NButton("Cancel",410,420);
        NButton ok = new NButton("Ok",320 ,420);
        add(ok,0);
        add(cancel,0);
        cancel.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    dispose();
                }
                });
        ok.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    acceptEdit();
                    dispose();
                    //FormWarning warn = new FormWarning("incomplete");
                    }
                });
    }
    public void initText(){
        Text code = new Text();        
        Text name = new Text();
        Text terms = new Text();
        Text contactPerson = new Text();
        Text telNo = new Text();
        Text begBal = new Text();
        Text apBal = new Text();
        Text address = new Text();
        Text fax = new Text();

        code.inText("Code:", 15, 15);
        name.inText("Name:", 15, 40);
        terms.inText("Terms:", 15, 65);
        begBal.inText("BEG. BAL:", 15, 90);
        apBal.inText("AP. BAL:", 15, 115);

        address.inText("Address:", 15, 170);

        contactPerson.inText("Contact Person:",15 , 225);
        telNo.inText("Tel. No.:", 15, 250);
        fax.inText("Fax:", 15, 275);

        add(code,0);
        add(name,0);
        add(address,0);
        add(terms,0);
        add(telNo,0);
        add(begBal,0);
        add(apBal,0);
        add(fax,0);
        add(contactPerson,0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void initFlds() {
        String query = "SELECT * FROM supplier WHERE suppCode = "+id;
        String qCode;
        String qName;
        String qTerms;
        String qBegBal;
        //String qApBal;
        String qAddr;
        String qConPer;
        String qTelNo;
        String qFaxNo;

        name.initText(60, 25, 100, 40);
        terms.initText(60, 25, 100, 65);
        begBal.initText(60, 25, 100, 90);
        //apBal.initText(60, 25, 100, 115);

        address.initText(250, 25, 100, 170);

        contactPerson.initText(200, 25, 130, 223);
        telNo.initText(60, 25, 100, 248);
        fax.initText(60, 25, 100, 273);

        try{
            Connection connection = Sql.getSqlConnection();
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(query);

            if(rs.next())
            {
                qCode = rs.getString("suppCode");
                qName = rs.getString("suppName");
                qTerms = rs.getString("suppTerms");
                qBegBal = rs.getString("suppBegBal");
                //qApBal
                qAddr = rs.getString("suppAddr");
                qConPer = rs.getString("suppConPerson");
                qTelNo = rs.getString("suppTelNo");
                qFaxNo = rs.getString("suppFaxNo");

                cd.inText(qCode, 100, 15);
                name.setText(qName);
                terms.setText(qTerms);
                begBal.setText(qBegBal);
                address.setText(qAddr);
                contactPerson.setText(qConPer);
                telNo.setText(qTelNo);
                fax.setText(qFaxNo);
            }

            Sql.closeSqlConnection(connection);
        } catch(Exception ex){
            Logger.getLogger(EditChassis.class.getName()).log(Level.SEVERE, null, ex);
        }

        add(cd,0);
        add(name,0);
        add(address,0);
        add(terms,0);
        add(telNo,0);
        add(begBal,0);
        //add(apBal,0);
        add(fax,0);
        add(contactPerson,0);
    }

    private void acceptEdit()
    {
        String query = "UPDATE supplier SET suppName='"+name.getText()+"', suppTerms='"+terms.getText()+
                "', suppBegBal='"+begBal.getText()+"', suppAddr='"+address.getText()+"', suppConPerson='"+contactPerson.getText()+
                "', suppTelNo='"+telNo.getText()+"', suppFaxNo='"+fax.getText()+"' WHERE suppCode="+id;

        try{
            Connection connection = Sql.getSqlConnection();
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.executeUpdate();

            Sql.closeSqlConnection(connection);
        } catch(Exception ex)
        {
            Logger.getLogger(EditCustomer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void centerScreen(){

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    // Determine the new location of the window
    int w = getSize().width;
    int h = getSize().height;
    int x = (dim.width-w)/2;
    int y = (dim.height-h)/2;

    // Move the window
    setLocation(x, y);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
